//
//  YHIMUIKitTextCell.m
//  YHChat
//
//  Created by apple on 2019/2/25.
//  Copyright © 2019 yinhe. All rights reserved.
//

#import "YHIMUIKitTextCell.h"
#import <YYText/YYText.h>

#import "YHIMUIKitHelper.h"
#import "YHIMUIKitMessageCellData.h"

@interface YHIMUIKitTextCell()
@property (nonatomic, strong) ASImageNode *bubbleImageNode;
@property (nonatomic, strong) ASDisplayNode *contentNode;
@property (nonatomic, strong) YYLabel *contentLabel;
@end

@implementation YHIMUIKitTextCell

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        self.bubbleImageNode = [[ASImageNode alloc] init];
        [self addSubnode:self.bubbleImageNode];
        
        __weak typeof(self) weakSelf = self;
        self.contentNode = [[ASDisplayNode alloc] initWithViewBlock:^UIView * _Nonnull{
            weakSelf.contentLabel = [[YYLabel alloc] init];
            weakSelf.contentLabel.numberOfLines = 0;
            return weakSelf.contentLabel;
        }];
        [self.bubbleImageNode addSubnode:self.contentNode];
        
        self.bubbleImageNode.layoutSpecBlock = ^ASLayoutSpec * _Nonnull(__kindof ASDisplayNode * _Nonnull node, ASSizeRange constrainedSize) {
            return [ASInsetLayoutSpec insetLayoutSpecWithInsets:YHIMUIKit_BubbleInset child:weakSelf.contentNode];
        };
        
        
        self.contentNode.backgroundColor = [UIColor orangeColor];
        self.bubbleImageNode.backgroundColor = [UIColor cyanColor];
    }
    return self;
}


- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize{
    
    if (!self.cellData.isSlef) {
        ASStackLayoutSpec *messageStackLayoutSpec = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:10 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsCenter children:@[self.bubbleImageNode, self.stateNode]];
        
        ASStackLayoutSpec *mainStackLayoutSpec = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:(self.cellData.showName ? 10.0 : 0.0) justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStart children:(self.cellData.showName ? @[self.nameNode, messageStackLayoutSpec] : @[messageStackLayoutSpec])];
        
        ASStackLayoutSpec *finalLayoutSpec = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:12 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStart children:@[self.headImageNode, mainStackLayoutSpec]];
        
        return [ASInsetLayoutSpec insetLayoutSpecWithInsets:YHIMUIKit_CellInset child:finalLayoutSpec];
        
    } else {
        ASStackLayoutSpec *messageStackLayoutSpec = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:10 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsCenter children:@[self.stateNode, self.bubbleImageNode]];
        
        ASStackLayoutSpec *mainStackLayoutSpec = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:(self.cellData.showName ? 10.0 : 0.0) justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsEnd children:(self.cellData.showName ? @[self.nameNode, messageStackLayoutSpec] : @[messageStackLayoutSpec])];
        
        ASStackLayoutSpec *finalLayoutSpec = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:12 justifyContent:ASStackLayoutJustifyContentEnd alignItems:ASStackLayoutAlignItemsStart children:@[mainStackLayoutSpec, self.headImageNode]];
        
        return [ASInsetLayoutSpec insetLayoutSpecWithInsets:YHIMUIKit_CellInset child:finalLayoutSpec];
    }
}


#pragma mark ------------------ Setter ------------------
- (void)setCellData:(YHIMUIKitMessageTextCellData *)cellData{
    super.cellData = cellData;
    
    if (cellData.isSlef) {
        self.bubbleImageNode.image = [[UIImage imageNamed:YHIMUIKit_Resource(@"sender_text_normal")] resizableImageWithCapInsets:UIEdgeInsetsFromString(@"{18,25,17,25}") resizingMode:UIImageResizingModeStretch];
    } else {
        self.bubbleImageNode.image = [[UIImage imageNamed:YHIMUIKit_Resource(@"receiver_text_normal")] resizableImageWithCapInsets:UIEdgeInsetsFromString(@"{18,25,17,25}") resizingMode:UIImageResizingModeStretch];
    }
    
    NSMutableAttributedString *atr = [YHIMUIKitHelper getMessageAttributedStringWith:cellData.text];
    self.contentLabel.attributedText = atr;
    
    CGFloat maxWidth = YHIMUIKit_ScreenWidth - 30 - YHIMUIKit_HeadSize.width - 100;
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:CGSizeMake(maxWidth, YHIMUIKit_ScreenHeight) text:atr];
    
    self.contentNode.style.preferredSize = layout.textBoundingSize;
}

@end
